// pages/examine_task_page.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:sjx/repository/sjx_api.dart';

import 'examine_task_model.dart';
import 'item/examine_task_item_page.dart';

class ExamineTaskPage extends StatefulWidget {
  final String taskNo;

  const ExamineTaskPage({Key? key, required this.taskNo}) : super(key: key);

  @override
  _ExamineTaskPageState createState() => _ExamineTaskPageState();
}

class _ExamineTaskPageState extends State<ExamineTaskPage> with SingleTickerProviderStateMixin {
  late TabController _tabController;
  late ExamineTaskProvider _provider;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 3, vsync: this);
    _provider = ExamineTaskProvider(taskNo: widget.taskNo);
    _loadTitleData();
  }

  Future<void> _loadTitleData() async {
    try {
      final response = await SJXApi.instance().taskPublishOrderStatics(widget.taskNo);
      _provider.updateTitleData(response);
    } catch (e) {
      print('加载标题数据失败: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider.value(
      value: _provider,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('审核任务'),
          leading: IconButton(
            icon: const Icon(Icons.arrow_back_ios),
            onPressed: () => Navigator.of(context).pop(),
          ),
          bottom: PreferredSize(
            preferredSize: const Size.fromHeight(40),
            child: Consumer<ExamineTaskProvider>(
              builder: (context, provider, child) {
                return TabBar(
                  controller: _tabController,
                  labelColor: Theme.of(context).primaryColor,
                  unselectedLabelColor: Colors.grey,
                  labelStyle: const TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.bold,
                  ),
                  indicator: BoxDecoration(
                    border: Border(
                      bottom: BorderSide(
                        color: Theme.of(context).primaryColor,
                        width: 4,
                      ),
                    ),
                  ),
                  tabs: [
                    Tab(text: '待审核（${provider.readyToApproveCount}）'),
                    Tab(text: '已通过（${provider.approvedCount}）'),
                    Tab(text: '已拒绝（${provider.rejectCount}）'),
                  ],
                );
              },
            ),
          ),
        ),
        body: TabBarView(
          controller: _tabController,
          children: [
            ExamineTaskItemPage(
              type: ExamineTaskType.daishenhe,
              taskNo: widget.taskNo,
              onReload: _loadTitleData,
            ),
            ExamineTaskItemPage(
              type: ExamineTaskType.yitongguo,
              taskNo: widget.taskNo,
              onReload: _loadTitleData,
            ),
            ExamineTaskItemPage(
              type: ExamineTaskType.yijujue,
              taskNo: widget.taskNo,
              onReload: _loadTitleData,
            ),
          ],
        ),
      ),
    );
  }
}

class ExamineTaskProvider with ChangeNotifier {
  final String taskNo;
  int readyToApproveCount = 0;
  int approvedCount = 0;
  int rejectCount = 0;

  ExamineTaskProvider({required this.taskNo});

  void updateTitleData(Map<String, dynamic> data) {
    readyToApproveCount = data['readyToApproveCount'] ?? 0;
    approvedCount = data['approvedCount'] ?? 0;
    rejectCount = data['rejectCount'] ?? 0;
    notifyListeners();
  }
}